<?php
/**
 * Fang_Service_Cate
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-15 17:59:03
 * 修改记录: 
 * 
 * $Id: Cate.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Service_Cate {

    /**
     * add 
     * 
     * @param  mixed $title 
     * @param  integer $level 
     * @param  mixed $link 
     * @param  integer $parentId 
     * @return void
     */
    public function add($title, $level = 0, $link = null, $parentId = 0) {

        $data = array('c_title' => $title,
                      'c_level' => $level,
                      'c_link' => $link,
                      'c_parent_id' => $parentId);
        try {
            return Fang_DAO_Cate::add($data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * update 
     * 
     * @param  mixed $cid 
     * @param  mixed $title 
     * @param  integer $level 
     * @param  mixed $link 
     * @param  integer $parentId 
     * @return void
     */
    public function update($cid, $title = null, $level = 0, $link = null, $parentId = 0) {

        $data = array('c_title' => $title,
                      'c_level' => $level,
                      'c_link' => $link,
                      'c_parent_id' => $parentId);
        try {
            return Fang_DAO_Cate::update($cid, $data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * getInfo 
     * 
     * @param  mixed $cid 
     * @param  array $fields 
     * @return void
     */
    public function getInfo($cid, $fields = array()) {

        try {
            return Fang_DAO_Cate::getInfo($cid, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listAll 
     * 
     * @return void
     */
    public function listAll() {

        try {
            $cates = Fang_DAO_Cate::listAll();
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }

        if (!$cates) {
            return array();
        }

        $res = array();
        foreach ($cates as $c) {
            if (!$c['c_parent_id']) {
                $res[$c['cid']]['data'] = $c;
            }
            if ($c['c_parent_id']) {
                $res[$c['c_parent_id']]['subs'][] = $c;
            }
        }

        return $res;
    }

    /**
     * remove 
     * 
     * @param  mixed $cid 
     * @return void
     */
    public function remove($cid) {

        try {
            $res = Fang_DAO_Cate::remove($cid);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }

        return $res;
    }

    /**
     * listByParentId 
     * 
     * @param  integer $parentId 
     * @return void
     */
    public function listByParentId($parentId = 0) {

        try {
            return Fang_DAO_Cate::listByParentId($parentId);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }
}
